C++复习 struct和typedef struct

  1. struct 和 typedef struct
    1. C 中
    2. C++ 中
  2. 类型别名
    1. typedef
    2. using
    3. 对于符合符合类型的别名的声明语句要小心

struct 和 typedef struct

C 中

// c
typedef struct Student {
    int age; 
} S;

等价于

// c
struct Student { 
    int age; 
};

typedef struct Student S;

此时 S 等价于 struct Student,但两个标识符名称空间不相同。

另外还可以定义与 struct Student 不冲突的 void Student() {}

C++ 中

由于编译器定位符号的规则(搜索规则)改变,导致不同于C语言。

一、如果在类标识符空间定义了 struct Student {...};,使用 Student me; 时,编译器将搜索全局标识符表,Student 未找到,则在类标识符内搜索。

即表现为可以使用 Student 也可以使用 struct Student,如下:

// cpp
struct Student { 
    int age; 
};

void f( Student me );       // 正确,"struct" 关键字可省略

二、若定义了与 Student 同名函数之后,则 Student 只代表函数,不代表结构体,如下:

typedef struct Student { 
    int age; 
} S;

void Student() {}           // 正确,定义后 "Student" 只代表此函数

//void S() {}               // 错误,符号 "S" 已经被定义为一个 "struct Student" 的别名

int main() {
    Student(); 
    struct Student me;      // 或者 "S me";
    return 0;
}

类型别名

typedef

typedef double wages; //wages是double的别名
typedef wages base,*p;//base是double的别名,p是double*的别名

using

  • c++11推荐用一下语法
using wages = double; //wages是double的别名
//using 别名 = 类型

对于符合符合类型的别名的声明语句要小心

typedef char *pstring;
const pstring cstr = 0; // pstring是一个整体,是个指向char的指针。const修饰他。所以它本身是一个常量。所以它是常量指针(const pointer)
const char *cstr; //而这里*修饰const char,所以它是指向char常量的指针,即指针常量(pointer to const)